package tech.abing.spider.task.crawler.amac;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.AdminLicenseProcessor;
import tech.abing.spider.task.pageprocessor.amac.AdminPunishProcessor;
import tech.abing.spider.task.pageprocessor.amac.BlackListProcessor;
import tech.abing.spider.task.pipeline.amac.AdminLicensePipeline;
import tech.abing.spider.task.pipeline.amac.AdminPunishPipeline;
import tech.abing.spider.util.SqlUtil;

public class AdminPunishSpider {
	
	private static SqlUtil sqlUtil = null;
	
	public static void start() {
		CrawlManager manager = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = manager
		.prepareCrawlTask(DictEnum.TASK_TYPE_ADMIN_PUNISH.getName(), DefaultPageDownloader.class)
		.useThread(1)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(1000)
		.usePipeline(AdminPunishPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_ADMIN_PUNISH.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_ADMIN_PUNISH_DETAIL.getCode());
//				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_ATTACHMENT.getCode()+" where 1=1 and ATTACH_TYPE = ?",TaskConstants.TASK_BLACKLIST);
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		manager.start(task);
	}
	
//	public static final class DynamicEntranceImpl extends DynamicEntrance{
//
//		@Override
//		public List<StartContext> loadStartContext() {
//			StartContext context = new StartContext();
//			String url = "http://www.csrc.gov.cn/pub/beijing/bjxzcf/";
//			
//			PageRequest req = context.createPageRequest(url, AdminPunishProcessor.class);
//			req.putHeader("User-Agent", ":Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
//			req.putHeader("Referer", "http://www.csrc.gov.cn/pub/beijing/");
//			req.putHeader("Host", "www.csrc.gov.cn");
//			req.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
//			req.putHeader("Cache-Control", "no-cache");
//			req.putHeader("Upgrade-Insecure-Requests", "1");
//			req.putHeader("Cookie", "_gscu_516223281=0164042185ybd321; _gscs_516223281=02068337ah2pqb21|pv:2; _gscbrs_516223281=1");
////			req.putHeader("If-Modified-Since", "Sun, 06 Aug 2017 10:07:13 GMT");
//			req.putHeader("If-None-Match", "12000000070266-37ad-55612e4efe195");
//			
//			context.injectSeed(req);
//			
//			return Arrays.asList(context);
//		}
//		
//	}

	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			List<String> urlList = new ArrayList<String>();
			urlList.add("http://www.csrc.gov.cn/pub/beijing/bjxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/beijing/xxfw/bjfxjs/");
			urlList.add("http://www.csrc.gov.cn/pub/tianjin/xzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/shanxi/xzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/neimenggu/nmgxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/liaoning/lnjxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/jilin/jljgcs/");
			urlList.add("http://www.csrc.gov.cn/pub/jilin/jlxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/shanghai/xzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/jiangsu/jsxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/anhui/ahxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/fujian/fjjxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/jiangxi/jxxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/shandong/sdxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/henan/hnxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/hubei/hbxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/hunan/hnjxzjgcs/");
			urlList.add("http://www.csrc.gov.cn/pub/guangdong/gdjjgcs/");
			urlList.add("http://www.csrc.gov.cn/pub/guangdong/xzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/hainan/hnjxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/chongqing/cqjxzcf/");			
//			urlList.add("http://www.csrc.gov.cn/pub/zjhpublicofcq/index.htm?channel=3284/3668");
			urlList.add("http://www.csrc.gov.cn/pub/sichuan/scxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/guizhou/gzxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/qinghai/qhxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/xinjiang/xjxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/shenzhen/xzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/dalian/dlxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/ningbo/nbxzcf/");
			urlList.add("http://www.csrc.gov.cn/pub/zhejiang/zjcxxx/");
			urlList.add("http://www.csrc.gov.cn/pub/zhejiang/zjxzcf/");
			
			for (String url : urlList) {
				PageRequest req = buildReq(context, url);
				context.injectSeed(req);
			}
			
			return Arrays.asList(context);
		}
		
		private PageRequest buildReq(StartContext context, String url) {
			PageRequest req = context.createPageRequest(url, AdminPunishProcessor.class);
			req.putHeader("User-Agent", ":Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
			req.putHeader("Referer", "http://www.csrc.gov.cn/pub/beijing/");
			req.putHeader("Host", "www.csrc.gov.cn");
			req.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control", "no-cache");
			req.putHeader("Upgrade-Insecure-Requests", "1");
			req.putHeader("Cookie", "_gscu_516223281=0164042185ybd321; _gscs_516223281=02068337ah2pqb21|pv:2; _gscbrs_516223281=1");
//			req.putHeader("If-Modified-Since", "Sun, 06 Aug 2017 10:07:13 GMT");
			req.putHeader("If-None-Match", "12000000070266-37ad-55612e4efe195");
			
			return req;
		}
		
	}
}
